<?php

include_once ("annotationList.php");
include_once ("mysqllib.php");
include_once ("slideList.php");

class video {

	private $db;
	private $idVideo;
	private $title;
	private $url;
	private $name;
	private $postingDate;
	private $annotationList;
	private $extensions;
	private $slides;

	//Return true if the video is added, and sets the current video object with the video added.
	public function addVideo($title, $name, $url, $idCourse) {

		$db = new mysqllib();

		$nowDate = date('Y-m-d H:i:s');

		$query = "INSERT INTO Video (titleVideo, nameVideo, pathVideo, postingDate, idCourse) 
				  VALUES ('$title', '$name','$url', '$nowDate', '$idCourse')";
		$result = mysql_query($query);

		//If the insert worked properly
		if ($result) {
			//get the id of the newly created annotation.
			$query2 = "SELECT idVideo FROM Video where 
					  titleVideo = '$title' AND nameVideo = '$name' AND pathVideo='$url'
					  AND postingDate = '$nowDate' AND idCourse = '$idCourse'";

			$result2 = mysql_query($query2);
			$data2 = mysql_fetch_array($result2);
			$idVideo = $data2['idVideo'];

			//set the private variables

			$this -> idVideo = $idVideo;
			$this -> title = $title;
			$this -> url = $url;
			$this -> name = $name;
			$this -> postingDate = $nowDate;

			//set the extensiosn of the video
			$this -> extensions = $this -> getExtensions();

			//set the slides of the video
			$this -> slides = new slideList($this -> idVideo);

		}
		
		return $result;
	}

	//Sets the current video object with the video where $title ='title'
	public function getVideoByTitle($title) {
		//connect to database
		$db = new mysqllib();

		//Create and send query to get the video
		$query = "SELECT * FROM Video WHERE titleVideo = '$title'";
		$result = mysql_query($query);
		$data = mysql_fetch_array($result);

		//Fill the current object with the fetched video's informations.

		$this -> idVideo = $data['idVideo'];
		$this -> title = $data['titleVideo'];
		$this -> url = $data['pathVideo'];
		$this -> name = $data['nameVideo'];
		$this -> postingDate = $data['postingDate'];

		//set the list of annotations related to the video.
		$this -> annotationList = new annotationList($this -> idVideo);

		//set the slides of the video
		$this -> slides = new slideList($this -> idVideo);

		//set the extensions of the videos
		$this -> extensions = $this -> getExtensions();

	}

	public function setVideo($idVideo, $title, $url, $name, $postingDate) {
		//set the private variables
		$this -> idVideo = $idVideo;
		$this -> title = $title;
		$this -> url = $url;
		$this -> name = $name;
		$this -> postingDate = $postingDate;

		//set the extensiosn of the video
		$this -> extensions = $this -> getExtensions();

		//set the slides of the video
		$this -> slides = new slideList($this -> idVideo);

	}

	public function getVideoById($idVideo) {
		//connect to database
		$db = new mysqllib();

		//Create and send query
		$query = "SELECT * FROM Video WHERE idVideo = '$idVideo'";
		$result = mysql_query($query);

		$data = mysql_fetch_array($result);

		//Fill the current object with the fetched video's informations.
		$this -> idVideo = $data['idVideo'];
		$this -> title = $data['titleVideo'];
		$this -> url = $data['pathVideo'];
		$this -> name = $data['nameVideo'];
		$this -> postingDate = $data['postingDate'];

		//set the list of annotations related to the video.
		$this -> annotationList = new annotationList($this -> idVideo);

		//set the slides of the video
		$this -> slides = new slideList($this -> idVideo);

		//set the extensions of the videos
		$this -> extensions = $this -> getExtensions();
	}

	public function sourcesVideo() {
		foreach ($this->extensions as $ext) {
			if ($ext == "ogv") {
				echo '<source src="' . $this -> getUrl() . $this -> getName() . '.' . $ext . '" type="video/ogg">';
			} else {
				echo '<source src="' . $this -> getUrl() . $this -> getName() . '.' . $ext . '" type="video/' . $ext . '">';
			}
		}
	}

	public function displayAnnotationsList($id) {
		$this -> annotationList -> displayAnnotationsForList($id);
	}

	public function displaySlides() {
		//javascript to show the right slide
		echo "<script>showSlide($this->idVideo);</script>";
		echo '<div id="slides">';
		//slides go there
		echo '</div>';
	}

	public function displayAnnotationsPins() {

		$video = new ffmpeg_movie(dirname(__FILE__) . '/' . $this -> url . $this -> name . '.mp4', FALSE);
		$this -> annotationList -> displayAnnotationsForPins($video -> getDuration());
	}
	
	public function displaySlidesPins(){
		$video = new ffmpeg_movie(dirname(__FILE__) . '/' . $this -> url . $this -> name . '.mp4', FALSE);
		$this->slides->displayAllSlidesPins($video -> getDuration());
	}

	public function getExtensions() {
		$ext = array();
		$directory = new DirectoryIterator(dirname(__FILE__) . '/' . $this -> url);

		//get the extensiosn of all the files in the video folder
		foreach ($directory as $fileinfo) {
			if ($fileinfo -> isFile()) {
				array_push($ext, $fileinfo -> getExtension());
			}
		}
		return $ext;
	}

	//Getters
	public function getIdVideo() {
		return $this -> idVideo;
	}

	public function getUrl() {
		return $this -> url;
	}

	public function getName() {
		return $this -> name;
	}

	public function getTitle() {
		return $this -> title;
	}

}
?>
